<?php

class LogsAction extends Action {
	
	private $cLogs 		= null;
    private $cAdmin		= null;
    private $cOrder 	= null;

	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
	
		import('@.ORG.Admin');
        import('@.ORG.Logs');
        import('@.ORG.Order');
    	
    	
		$this->cAdmin 		= new Admin();
		$this->cLogs		= new Logs();
        $this->cOrder    	= new Order();
	}
	
	//列表
    public function index(){

    	admin_role('Logs:index');
    	
    	$begin_time	= strtotime(date('Y-m-d'));
    	$end_time 	= strtotime(date('Y-m-d')) + 86400 - 1;
    	
    	$get = get(
    		array(
	    		array('begin_time'			,''	,full_date($begin_time)),
	    		array('end_time'			,''	,full_date($end_time)),
	    		array('admin_id'			,'intval'	,0),
	    		array('p'				,'intval'	,1)
    		)
    	);
    	
    	$pageSize = 30;
    	$allCount = 0;
    	
    	$page	= $get['p'];

    	$where = $get;
    	    	
    	$logs = $this->cLogs->lists($where,$page,$pageSize,$allCount);
    	    	
    	//开始时间
    	$begin_time	= $get['begin_time'];
    	//结束时间
    	$end_time	= $get['end_time'];
    	
    	//管理员
    	$admins = $this->cAdmin->lists();
    	
    	$admin_id	= $get['admin_id'];
    	
    	
    	/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
		//导入分页类
		import("@.ORG.Page");
		
		$params = $get;
		
		unset($params['p']);
		
		$params['g'] = 'Admin';
		
		// 实例化分页类
	  	$p = new Page($allCount,$pageSize,$params);
	  	
    	// 分页显示输出
    	$pager = $p->show();

		/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
    	    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,    		
    			'begin_time'	=> $begin_time,
    			'end_time'		=> $end_time,
    			'admin_id'		=> $admin_id,
    			'admins'		=> $admins,
    			'logs'			=> $logs,
    			'pager'			=> $pager,
    			'MainPage'		=> 'Logs:index'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }

    //列表
    public function order_received(){

        admin_role('Logs:index');

        $get = get(
            array(
                array('begin_time'			,''	,short_date()),
                array('end_time'			,''	,short_date()),
                array('p'				,'intval'	,1)
            )
        );

        $begin_time = $get['begin_time'];
        $end_time   = $get['end_time'];

        $pageSize = 200;
        $allCount = 0;

        $page	= $get['p'];

        $orders = $this->cOrder->order_received_log($begin_time,$end_time,$page,$pageSize,$allCount);

        //开始时间
        $begin_time	= $get['begin_time'];
        //结束时间
        $end_time	= $get['end_time'];

        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */
        //导入分页类
        import("@.ORG.Page");

        $params = $get;

        unset($params['p']);

        $params['g'] = 'Admin';

        // 实例化分页类
        $p = new Page($allCount,$pageSize,$params);

        // 分页显示输出
        $pager = $p->show();

        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */


        //待收货订单
        $receive_submit_count = $this->cOrder->get_receive_submit_order_count();

        //5.收货中的商品
        $receving_goods = $this->cOrder->get_one_receiving_goods();

        //收货中的商品数量
        $receving_goods_count = count($receving_goods);

        //收货中的唯一商品数量
        $receving_goods_unique = count(array_unique($receving_goods));

        //计算每小时收货几单
        $today = date('Y-m-d');

        //平均每单多少秒
        $avg_order_time = 0;

        //显示预计时间
        $is_show_plan = 0;

        //每小时多少单
        $hour_order_count  = 0;

        //预计用时
        $plan_use_time = 0;


        if($begin_time == $today && $end_time == $today){
            $second_span = time() - strtotime($today);
            if($allCount > 0){
                $is_show_plan = 1;
                $avg_order_time = round($second_span / $allCount);

                //预计用时
                $plan_use_time = round(($receive_submit_count * $avg_order_time) / 3600,2);

                //每小时多少单
                $hour_order_count = ceil(3600 / $avg_order_time);

            }
        }

        $this->assign(
            array(
                'APP_NAME'		=> APP_NAME,
                'begin_time'	=> $begin_time,
                'end_time'		=> $end_time,
                'orders'		=> $orders,
                'pager'			=> $pager,
                'is_show_plan'=> $is_show_plan,

                'plan_use_time'=> $plan_use_time,

                'hour_order_count'=> $hour_order_count,
                'avg_order_time'    => $avg_order_time,

                'receving_goods_count' => $receving_goods_count,
                'receving_goods_unique'=> $receving_goods_unique,
                'receive_submit_count' => $receive_submit_count,
                'MainPage'		=> 'Logs:order_received'
            )
        );

        $this->display(C('ADM_TEMPLATE'));
    }


    //列表
    public function order_commented(){

        admin_role('Logs:index');

        $get = get(
            array(
                array('begin_time'			,''	,short_date()),
                array('end_time'			,''	,short_date()),
                array('p'				,'intval'	,1)
            )
        );

        $begin_time = $get['begin_time'];
        $end_time   = $get['end_time'];

        $pageSize = 200;
        $allCount = 0;

        $page	= $get['p'];

        $orders = $this->cOrder->order_commented_log($begin_time,$end_time,$page,$pageSize,$allCount);

        //开始时间
        $begin_time	= $get['begin_time'];
        //结束时间
        $end_time	= $get['end_time'];

        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */
        //导入分页类
        import("@.ORG.Page");

        $params = $get;

        unset($params['p']);

        $params['g'] = 'Admin';

        // 实例化分页类
        $p = new Page($allCount,$pageSize,$params);

        // 分页显示输出
        $pager = $p->show();

        /**
        +------------------------------------------------------------------------------
         * 分页
        +------------------------------------------------------------------------------
         */


        //待收货订单
        $comment_submit_count = $this->cOrder->get_comment_submit_order_count();

        //5.收货中的商品
        $commenting_goods = $this->cOrder->get_one_commenting_goods();

        //收货中的商品数量
        $commenting_goods_count = count($commenting_goods);

        //收货中的唯一商品数量
        $commenting_goods_unique = count(array_unique($commenting_goods));

        //计算每小时收货几单
        $today = date('Y-m-d');

        //平均每单多少秒
        $avg_order_time = 0;

        //显示预计时间
        $is_show_plan = 0;

        //每小时多少单
        $hour_order_count  = 0;

        //预计用时
        $plan_use_time = 0;


        if($begin_time == $today && $end_time == $today){
            $second_span = time() - strtotime($today);
            if($allCount > 0){
                $is_show_plan = 1;
                $avg_order_time = round($second_span / $allCount);

                //预计用时
                $plan_use_time = round(($comment_submit_count * $avg_order_time) / 3600,2);

                //每小时多少单
                $hour_order_count = ceil(3600 / $avg_order_time);

            }
        }

        $this->assign(
            array(
                'APP_NAME'		=> APP_NAME,
                'begin_time'	=> $begin_time,
                'end_time'		=> $end_time,
                'orders'		=> $orders,
                'pager'			=> $pager,
                'is_show_plan'=> $is_show_plan,

                'plan_use_time'=> $plan_use_time,

                'hour_order_count'=> $hour_order_count,
                'avg_order_time'    => $avg_order_time,

                'commenting_goods_count' => $commenting_goods_count,
                'commenting_goods_unique'=> $commenting_goods_unique,
                'comment_submit_count'  => $comment_submit_count,
                'MainPage'		=> 'Logs:order_commented'
            )
        );

        $this->display(C('ADM_TEMPLATE'));
    }

}